<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<input id="source1" value="Lorem ipsum">Lorem ipsum</input>
<input id="source2" value="http://example.com"></input>
<div id="destination" style="min-height:100px; border: solid 1px black">Drop text here if you test this manually</div>

<script>
description('This tests the basic functionality and properties of DataTransferItems for string data with drag and drop. This test requires DRT.')

window.jsTestIsAsync = true;

var testSources = [ 'source1', 'source2' ];
var testIndex = 0;
var expectedDroppedText = '';

function startTest()
{
    var destination = document.getElementById('destination');
    destination.addEventListener('dragover', handleDragOver, false);
    destination.addEventListener('drop', handleDrop, false);

    if (!window.testRunner)
        return;

    runNextTest();
}

function runNextTest()
{
    if (testIndex == testSources.length) {
        finishJSTest();
        return;
    }

    var sourceId = testSources[testIndex++];
    var source = document.getElementById(sourceId);
    expectedDroppedText = source.value;
    debug('Dragging text in ' + sourceId + ': ' + source.value);

    // Drag a text in the source element.
    source.focus();
    source.setSelectionRange(0, source.value.length);
    x = source.offsetLeft + 10;
    y = source.offsetTop + source.offsetHeight / 2;
    eventSender.mouseMoveTo(x, y);
    eventSender.mouseDown();

    // Drop it off to the destination field.
    var destination = document.getElementById("destination");
    eventSender.leapForward(500);
    eventSender.mouseMoveTo(destination.offsetLeft + 10, destination.offsetTop + destination.offsetHeight / 2);
    eventSender.mouseUp();
}

function handleDragOver(e)
{
    e.stopPropagation();
    e.preventDefault();
}

var currentItem;
function handleDrop(e)
{
    e.stopPropagation();
    e.preventDefault();

    debug('Verifying contents of DataTransferItems...');

    var items = e.dataTransfer.items;
    var remaining = items.length;

    for (var i = 0; i < items.length; ++i) {
        debug('items.length: ' + items.length);
        debug('items[' + i + '].kind: ' + items[i].kind);
        debug('items[' + i + '].type: ' + items[i].type);

        currentItem = items[i];
        shouldThrow('currentItem.getAsString()', '"TypeError: Failed to execute \'getAsString\' on \'DataTransferItem\': 1 argument required, but only 0 present."');
        shouldNotThrow('currentItem.getAsString(null)');
        items[i].getAsString(function(data) {
            window.stringData = data;
            shouldBeEqualToString('stringData', expectedDroppedText);
            if (--remaining == 0 && window.testRunner)
                runNextTest();
        });
    }
}

startTest();

</script>
</body>
</html>
